package com.ray.test.data;

import com.ray.test.proxy.MockTestConfig;
import com.ray.test.serializable.Serializable;
import lombok.extern.slf4j.Slf4j;

import java.io.File;

/**
 * @author bo shen
 * @Description: mock数据转换
 * @Class: DataChange
 * @Package com.ray.test.data
 * @date 2020/3/12 8:35
 * @company <p>杭州传化陆鲸科技有限公司</p>
 * @updateRecord time(修改时间)  author(修改人)   desc(修改内容)
 */
@Slf4j
public class DataChange {

    private Class<? extends Serializable> in;

    private Class<? extends Serializable> out;

    private Boolean deleteSourceFile = false;

    String dataPath = "mock";
    /***测试跟目录**/
    String testRoot = "src/test/resources";

    public DataChange(Class<? extends Serializable> in, Class<? extends Serializable> out, String dataPath, String testRoot) {
        this.in = in;
        this.out = out;
        this.dataPath = dataPath;
        this.testRoot = testRoot;
    }

    public DataChange(Class<? extends Serializable> in, Class<? extends Serializable> out) {
        this.in = in;
        this.out = out;
    }

    public DataChange(Class<? extends Serializable> in, Class<? extends Serializable> out,Boolean deleteSourceFile) {
        this.in = in;
        this.out = out;
        this.deleteSourceFile = deleteSourceFile;
    }

    /**
     * 数据转换
     */
    public void doChange() {
        try {
            if (in == null || out == null) {
                log.info("参数配置错误");
                return;
            }
            Serializable inSeraializable = (Serializable) in.newInstance();
            Serializable outSeraializable = (Serializable) out.newInstance();
            //读取文件信息
            File file = new File(String.format("%s/%s", testRoot, dataPath));
            if (!file.exists()) {
                log.info("文件目录不存在:{}/{}", testRoot, dataPath);
                return;
            }
            for (File cfile : file.listFiles()) {
                log.info("读取文件目录:{}", cfile.getName());
                MockTestConfig config = new MockTestConfig();
                config.setTestRoot(testRoot);
                config.setDataPath(dataPath);
                if (cfile.isDirectory()) {
                    config.setClassName(cfile.getName());
                    for (File dataFile : cfile.listFiles()) {
                        if (dataFile.isFile()) {
                            String fileName  = dataFile.getName();
                            log.info("转换文件:{}",fileName);
                            config.setName(fileName.substring(0, fileName.lastIndexOf(".")));
                            try {
                                config = inSeraializable.get(config);
                                outSeraializable.set(config);
                            }catch (Exception e){
                              log.info("文件：{}数据转换异常:{}",fileName,e.getMessage());
                            }
                        }
                    }
                }
                if(deleteSourceFile){
                    inSeraializable.deleteFile(config);
                }
            }
        } catch (Exception e) {
            log.error("数据转换准备异常:{}", e.getMessage());
        }
    }

}
